package com.own.toyexchange;

/*
 * 用户未读申请的列表界面
 */

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bjtu.toyexchange.R;
import com.request.toyexchange.ReceRequest;
import com.util.toyexchange.DialogUtil;
import com.util.toyexchange.HttpUtil;

public class OwnNotRea extends Activity {
	private ListView listview;
	private LayoutInflater layout;
	private String username;
	private Button fanhui;
	private JSONArray jsonArr;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.not_read);
		
		Intent intent = getIntent();
		//得到本用户用户名
		username = intent.getStringExtra("username");
		
		//向数据库传值，与服务器连接
		connectServer();
		
		//返回的按钮
		fanhui = (Button)findViewById(R.id.not_back_btn);
		fanhui.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				OwnNotRea.this.finish();
			}
		});
		
		findViewById();
	}
	
	protected void findViewById() {
		listview = (ListView)this.findViewById(R.id.notList);
		listview.setAdapter(new CatergorAdapter());
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterview, View view, int parent,
					long id) {
				Intent intent = new Intent();
				String sender = mSenderValues[parent];
				String sender_phone = ua_phone[parent];
				int application_id = a_id[parent];
				intent.putExtra("sender", sender);
				intent.putExtra("username", username);
				intent.putExtra("sender_phone", sender_phone);
				intent.putExtra("application_id", application_id);
				intent.putExtra("ta_id", ta_id[parent]);
				intent.putExtra("tb_id", tb_id[parent]);
				intent.setClass(OwnNotRea.this, ReceRequest.class);
				OwnNotRea.this.startActivity(intent);
				
			}
		});
	}
	private class CatergorAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return jsonArr.length();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return jsonArr.optJSONObject(position);
		}

		@Override
		public long getItemId(int position) {
			try {
				return ((JSONObject)getItem(position)).getInt("t_id");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = new ViewHolder();
			layout = LayoutInflater.from(OwnNotRea.this);
			//组装数据
			if(convertView==null){
				convertView = layout.inflate(R.layout.xiaoxi_item, null);
				holder.pic = (ImageView)convertView.findViewById(R.id.mes_pic);
				holder.sender = (TextView)convertView.findViewById(R.id.mes_sender);
				holder.content = (TextView)convertView.findViewById(R.id.mes_info);
				//使用tag存储数据
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder)convertView.getTag();
			}
			holder.pic.setImageResource(mImageIds[position]);
			holder.sender.setText(mSenderValues[position]);
			holder.content.setText(mContentValues[position]);
			
			return convertView;
		}
	}

	// 适配显示的图片数组
	private Integer[] mImageIds = null;  //发送申请者的头像       暂时不从数据库中获取
	//给照片添加文字显示(Title)
	private String[] mSenderValues = null; //发送申请者的用户名
	private String[] mContentValues= null;  //发送申请者的申请内容      不从数据库中获取
	private Integer[] a_id = null;  //申请单的id
	private Integer[] ta_id = null;  //申请者的玩具id
	private Integer[] tb_id = null;  //被申请者的玩具id
	private Integer[] application_state = null;  //申请单的状态
	private String[] ua_phone = null; //申请者的电话
	private String[] ub_phone = null; //被申请者的电话
	
	public static class ViewHolder {
			ImageView pic;
			TextView sender;
			TextView content;
	}
	
	private boolean connectServer(){
		 boolean b = false;
		 String url = HttpUtil.BASE_URL + "ApplicationListServlet";
		 Map<String, String> map = new HashMap<String, String>();
		 map.put("username", username);
			try
			{
				jsonArr = new JSONArray(HttpUtil.postRequest(url,map)); 
				if (jsonArr!=null)
				{
					mSenderValues = new String[jsonArr.length()];
					mImageIds = new Integer[jsonArr.length()];
					a_id = new Integer[jsonArr.length()];
					ta_id = new Integer[jsonArr.length()];
					tb_id = new Integer[jsonArr.length()];
					application_state = new Integer[jsonArr.length()];
					ua_phone = new String[jsonArr.length()];
					ub_phone = new String[jsonArr.length()];
					mContentValues = new String[jsonArr.length()];
					for(int i=0;i < jsonArr.length();i++){
						JSONObject jsonObject = (JSONObject)jsonArr.opt(i);
						mSenderValues[i] = jsonObject.getString("ua_name");
						a_id[i] = jsonObject.getInt("id");
						ta_id[i] = jsonObject.getInt("ta_id");
						tb_id[i] = jsonObject.getInt("tb_id");
						ua_phone[i] = jsonObject.getString("ua_phone");
						mContentValues[i] = "申请交换玩具";
						mImageIds[i] = R.drawable.pic;
					}
					
				}
				else
					DialogUtil.showDialog(this
							, "玩具列表更新失败或玩具列表为空！", false);
			}
			catch (Exception e)
			{
				DialogUtil.showDialog(this
					, "服务器响应异常，请稍后再试！", false);
				e.printStackTrace();
			}
			Log.v("2222", "ok");
		 return b;
	 }
}
